package service

import (
	"singo/model"
	"singo/serializer"
)

// ListDefectService 需求列表服务
type ListDefectService struct {
	Limit      int    `form:"pageSize"`
	Start      int    `form:"pageNo"`
	ProjectID  string `form:"project_id"`
	Done       uint   `form:"done"`
	UserID     string `form:"user_id"`
	Status     uint   `form:"status"`
	DeadlineAt string `form:"deadline_at"`
}

// DefectList 需求列表
func (service *ListDefectService) DefectList() serializer.Response {
	defects := []model.Defect{}
	defects2 := []model.Defect{}
	total := 0
	total2 := 0
	if service.Limit == 0 {
		service.Limit = 10
	}

	// 根据执行人相关查询
	if service.UserID != "" {
		if service.Done == 3 {
			if err := model.DB.Model(&defects).Where("execute_by LIKE ?", "%"+service.UserID+"%").Where(&model.Defect{Status: service.Done}).Count(&total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}

			if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.UserID+"%").Where(&model.Defect{Status: service.Done}).Order("updated_at desc, deadline_at").Find(&defects).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			return serializer.BuildListResponse(serializer.BuildDefects(defects), uint(total))
		} else {
			if err := model.DB.Model(&defects).Where("execute_by LIKE ?", "%"+service.UserID+"%").Not("status", 3).Count(&total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}

			if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.UserID+"%").Not("status", 3).Order("updated_at desc, priority").Find(&defects).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			return serializer.BuildListResponse(serializer.BuildDefects(defects), uint(total))
		}
	} else {
		if err := model.DB.Model(&defects).Where("project_id = ?", service.ProjectID).Not("status", 3).Not("status", 4).Count(&total).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("project_id = ?", service.ProjectID).Not("status", 3).Not("status", 4).Order("created_at desc").Find(&defects).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Model(&defects2).Where("project_id = ?", service.ProjectID).Not("status", 1).Not("status", 2).Count(&total2).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("project_id = ?", service.ProjectID).Not("status", 1).Not("status", 2).Order("status, complete_at desc").Find(&defects2).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		all_defects := []model.Defect{}
		all_defects = append(all_defects, defects...)
		all_defects = append(all_defects, defects2...)

		return serializer.BuildListResponse(serializer.BuildDefects(all_defects), uint(total+total2))
	}
}
